package cn.itcast.bos.web.action.base;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.constant.Constants;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.bos.web.action.common.BaseAction;
import cn.itcast.crm.domain.Customer;

@Controller
@Namespace("/")
@ParentPackage("struts-default")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class OrderAction extends BaseAction<Order> {

	private String sendAreaInfo;
	private String recAreaInfo;

	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}

	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}

	@Action(value = "addOrder", results = { @Result(name = "success", type = "redirect", location = "./index.html") })
	public String addOrder() {
		// 保存发件省市区及收件省市区数据
		String[] sendAreaArray = sendAreaInfo.split("/");
		Area sendArea = new Area();
		sendArea.setProvince(sendAreaArray[0]);
		sendArea.setCity(sendAreaArray[1]);
		sendArea.setDistrict(sendAreaArray[2]);

		String[] recAreaArray = recAreaInfo.split("/");
		Area recArea = new Area();
		recArea.setProvince(recAreaArray[0]);
		recArea.setCity(recAreaArray[1]);
		recArea.setDistrict(recAreaArray[2]);

		model.setSendArea(sendArea);
		model.setRecArea(recArea);

		// 添加客户id
		Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("existCustomer");
		model.setCustomer_id(customer.getId());

		// 发送数据到服务端
		WebClient.create(Constants.BOS_MANAGEMENT_URL + "/services/orderService/order").type(MediaType.APPLICATION_JSON)
				.post(model);
			
		return SUCCESS;
	}
}
